/* 
 *	Copyright 2014  Yanqing Wu
 *		email: yqwu_yqwu@126.com
 *
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
*/

#include "stdafx.h"
#include "StdCtrlSkinTool_Utils.h"

#include <string>

CStdCtrlSkin_Enums _cstdctrlskintool_enum_;

CStdCtrlSkin_Enums* StdCtrlSkinTool_GetEnum(void)
{
	return &_cstdctrlskintool_enum_;
}

CStdCtrlSkinAttrib StdCtrlSkinTool_MergeAttrib(CStdCtrlSkinAttrib attribInst, CStdCtrlSkinAttrib attribDef)
{
	auto result = attribInst;
	if ( attribInst.width<0 )
		result.width = attribDef.width;
	if ( attribInst.height<0 )
		result.height = attribDef.height;
	if ( attribInst.padx<0 )
		result.padx = attribDef.padx;
	if ( attribInst.pady<0 )
		result.pady = attribDef.pady;
	if ( attribInst.sticky==CStdCtrlSkin_EnumSticky::UNDEF )
		result.sticky = attribDef.sticky;
	return result;
}

CString StdCtrlSkinTool_XmlGetNodeName(CComPtr<IXMLDOMNode> spNode)
{
	CString name;
	CComBSTR bName;
	spNode->get_nodeName(&bName);
	name = (CString)bName;
	return name;
}

CString StdCtrlSkinTool_XmlGetNodeValue(CComPtr<IXMLDOMNode> spNode)
{
	VARIANT vValue;
	CString value;
	spNode->get_nodeValue(&vValue);
	// It should be a BSTR
	if ( vValue.vt==VT_BSTR )
	{
		value = (CString)vValue.bstrVal;
		SysFreeString(vValue.bstrVal);
	}
	else
		value.Empty();
	return value;
}

std::map<CString, CString> StdCtrlSkinTool_XmlGetAttribPairs(CComPtr<IXMLDOMNode> spNode)
{
	std::map<CString, CString> attribPairs;
	CComPtr<IXMLDOMNamedNodeMap> spMap;
	long length;
	spNode->get_attributes(&spMap);
	spMap->get_length(&length);
	for (long i=0; i!=length; ++i)
	{
		CComPtr<IXMLDOMNode> spNode;
		spMap->get_item(i, &spNode);
		auto name = StdCtrlSkinTool_XmlGetNodeName(spNode);
		auto value = StdCtrlSkinTool_XmlGetNodeValue(spNode);
		attribPairs[name] = value;
	}
	return attribPairs;
}

std::map<CString, CString> StdCtrlSkinTool_XmlGetAttribPairs(CComPtr<IXMLDOMElement> spNode)
{
	std::map<CString, CString> attribPairs;
	CComPtr<IXMLDOMNamedNodeMap> spMap;
	long length;
	spNode->get_attributes(&spMap);
	spMap->get_length(&length);
	for (long i=0; i!=length; ++i)
	{
		CComPtr<IXMLDOMNode> spNode;
		spMap->get_item(i, &spNode);
		auto name = StdCtrlSkinTool_XmlGetNodeName(spNode);
		auto value = StdCtrlSkinTool_XmlGetNodeValue(spNode);
		attribPairs[name] = value;
	}
	return attribPairs;
}

long StdCtrlSkinTool_CString2Long(CString str)
{
	CStringA tmpStr(str);
	int base = 10;
	//	Check to see whether it is hex mode (for color)
	auto left = str.Left(2);
	if ( !left.Compare(_T("0x")) || !left.Compare(_T("0X")) )
		base = 16;
	return std::stoi(tmpStr.GetString(), 0, base);
}

CImage* StdCtrlSkinTool_LoadImageFile(CString filename)
{
	CImage* pImg = new CImage;
	auto hr = pImg->Load(filename);
	if ( FAILED(hr) )
	{
		delete pImg;
		return nullptr;
	}
	if ( pImg->GetBPP()==32 )
	{
		//	It has alpha, do it
		for(int i =0; i<pImg->GetWidth(); i++)
			for(int j=0; j<pImg->GetHeight(); j++)
			{
				byte *pByte = (byte*)pImg->GetPixelAddress(i, j);
				pByte[0] = pByte[0] * pByte[3] / 255;
				pByte[1] = pByte[1] * pByte[3] / 255;
				pByte[2] = pByte[2] * pByte[3] / 255;
			}
	}
	return pImg;
}